package com.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.pojo.RoomType;
import com.service.RoomTypeService;
import com.util.ConstUtils;
import com.util.UUIDUtils;
import com.vo.DataVO;
import com.vo.RoomTypeVo;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author mornd
 * @date 2020/11/24 - 11:02
 */
@RestController
@RequestMapping("/roomType")
public class RoomTypeController {
    @Autowired
    private RoomTypeService roomTypeService;

    /**
     * 查询房型列表
     *
     * @param roomTypeVo
     * @return
     */
    @RequestMapping("/list")
    public DataVO<RoomType> findRoomTypeList(RoomTypeVo roomTypeVo) {
        PageHelper.startPage(roomTypeVo.getPage(), roomTypeVo.getLimit());
        PageInfo<RoomType> pageInfo = new PageInfo<RoomType>(roomTypeService.findRoomTypeList(roomTypeVo));
        return new DataVO<RoomType>(pageInfo.getTotal(), pageInfo.getList());
    }

    /**
     * 上传房型图片
     * @return
     */
    /*@RequestMapping("/uploadFile")
    public Map<String,Object> uploadFile(MultipartFile attach){
        //返回至前台对应的数据格式
        Map<String,Object> maps = new HashMap<String, Object>();
        //判断是否有文件
        if(!attach.isEmpty()){
            //指定文件上传地址
            String path = ConstUtils.ROOMTYPE_UPLOAD_PATH;
            //获取原文件名称
            String oldName = attach.getOriginalFilename();
            //获取文件后缀名
            String extension = FilenameUtils.getExtension(oldName);
            //重命名文件名称
            String newFileName = UUIDUtils.randomID() + "." + extension;
            //新建文件夹解决文件夹过多问题
            String datePath = new SimpleDateFormat("yyyyMMdd").format(new Date());
            //拼接最后的文件名
            String finalName = datePath + "/" + newFileName;
            //创建文件对象
            File destFile = new File(path,finalName);
            //判断文件夹是否存在 不存在则创建文件夹
            if(!destFile.getParentFile().exists()){
                destFile.getParentFile().mkdirs();
            }
            try {
                //保存文件至磁盘
                attach.transferTo(destFile);
                //保存相应的数据
                maps.put("code",0);
                maps.put("msg","");
                Map<String,Object> dataMaps = new HashMap<String, Object>();
                dataMaps.put("src","/hotel/show/roomTypeImages/" + finalName);//文件上传成功的回显地址
                maps.put("data",dataMaps);
                maps.put("imagePath",finalName);//图片名称，目的是给photo隐藏域赋值
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return maps;
    }*/

    /**
     * 添加房型
     *
     * @param roomType
     * @return
     */
    @RequestMapping("/add")
    public Map<String, Object> addRoomType(RoomType roomType) {
        Map<String, Object> maps = new HashMap<String, Object>();
        if (roomTypeService.addRoomType(roomType) > 0) {
            maps.put(ConstUtils.SUCCESS, true);
            maps.put(ConstUtils.MESSAGE, "添加成功！");
        } else {
            maps.put(ConstUtils.SUCCESS, false);
            maps.put(ConstUtils.MESSAGE, "添加失败，请重试！");
        }
        return maps;
    }

    /**
     * 修改房型
     *
     * @param roomType
     * @return
     */
    @RequestMapping("/update")
    public Map<String, Object> updateRoomType(RoomType roomType) {
        Map<String, Object> maps = new HashMap<String, Object>();
        if (roomTypeService.updateRoomType(roomType) > 0) {
            maps.put(ConstUtils.SUCCESS, true);
            maps.put(ConstUtils.MESSAGE, "修改成功！");
        } else {
            maps.put(ConstUtils.SUCCESS, false);
            maps.put(ConstUtils.MESSAGE, "修改失败，请重试！");
        }
        return maps;
    }

    /**
     * 根据房型id查询房间数量
     *
     * @param roomTypeId
     * @return
     */
    @RequestMapping("/checkRoomTypeHasRoom")
    public Map<String, Object> checkRoomTypeHasRoom(Integer roomTypeId) {
        Map<String, Object> maps = new HashMap<String, Object>();
        if (roomTypeService.getRoomCount(roomTypeId) > 0) {
            maps.put(ConstUtils.EXIST, true);
            maps.put(ConstUtils.MESSAGE, "该房型下已有房间，不可删除！");
        } else {
            maps.put(ConstUtils.EXIST, false);
        }
        return maps;
    }

    /**
     * 删除房型
     *
     * @param roomTypeId
     * @return
     */
    @RequestMapping("/del")
    public Map<String, Object> deleteRoomType(Integer roomTypeId, String deleteImagePath) {
        Map<String, Object> maps = new HashMap<String, Object>();
        if (roomTypeService.deleteRoomType(roomTypeId, deleteImagePath)) {
            maps.put(ConstUtils.SUCCESS, true);
            maps.put(ConstUtils.MESSAGE, "删除成功！");
        } else {
            maps.put(ConstUtils.SUCCESS, false);
            maps.put(ConstUtils.MESSAGE, "删除失败，请重试！");
        }
        return maps;
    }

    /**
     * 查询房型集合
     *
     * @return
     */
    @RequestMapping("/findRoomTypeList")
    public String findRoomTypeByRedis() {
        return roomTypeService.findRoomTypeByRedis();
    }
}
